<?php
namespace Admin\Controller;

use Admin\Controller\GlobalController;

class NewscateController extends GlobalController
{
    //杂志列表
    public function getlist()
    {
        $pageSize = 20;
        $where = array();
        $ncModel = M('news_cate');
        $count = $ncModel->where($where)->count();
        $pageObj = new \Admin\Util\Page($count, $pageSize);
        $list = $ncModel->where($where)->order('nc_id desc')->limit($pageObj->firstRow . ',' . $pageObj->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $pageObj->show());
        $this->display();
    }

    //添加或者修改
    public function save()
    {
        $ncModel = M('news_cate');
        if (IS_POST) {
            $nc_id = I('post.nc_id', 0, 'intval');
            $data['cate_name'] = I('post.cate_name', '', 'trim');
            if ($nc_id) {
                $ncModel->where(array('nc_id' => $nc_id))->save($data);
            } else {
                $ncModel->add($data);
            }
            $this->ajaxReturn('', 'success', 'TIP_SUCCESS');
        }
        $nc_id = I('get.nc_id', 0, 'intval');
        $info = $ncModel->where(array('nc_id' => $nc_id))->find();
        $this->assign('nc_id', $nc_id);
        $this->assign('info', $info);
        $this->display();
    }

    public function del()
    {
        $nc_id = I('post.nc_id', 0, 'intval');
        //先检查下面有没有期刊
        $newsModel = M('news');
        $rs = $newsModel->where(array('nc_id' => $nc_id, 'status' => 1))->find();
        if ($rs) {
            $this->ajaxReturn('', '分类下有已经上线的资讯不能删除', 'TIP_ERROR');
        }
        $ncModel = M('news_cate');
        $ncModel->where(array('nc_id' => $nc_id))->delete();
        $this->ajaxReturn('', 'success', 'TIP_SUCCESS');
    }
}